#!/usr/bin/python
# -*- coding:utf-8 -*-
# @FileName : python_test10_test2.py
# Author    : myh

# test2 把注册账户的姓名保存到文件中
from pathlib import Path
import json

username = input("What is your name? ")
path = Path('username.json')
try:
    contents = path.read_text(encoding='gbk')
except FileNotFoundError:
   print('出错了')
else:
    if len(contents) == 0:
        contents = []
    else:
        contents = json.loads(contents)
    print(contents)
    if username in contents:
        print('您已经注册过了')
    else:
        contents.append(username)
        contents = json.dumps(sorted(contents))
        path.write_text(contents)
        print(f"We'll remember you when you come back, {contents}!")



